package com.atguigu.windows;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.Date;

public class Flink06_ProcessFunction2 {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        //实验一下窗口的增量聚合函数
        env.socketTextStream("hadoop162",9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new WaterSensor(datas[0],Long.valueOf(datas[1]),Integer.valueOf(datas[2]));
                    }
                })
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                })
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                //我们这里求一个窗口中的水位平均值！
                .aggregate(new AggregateFunction<WaterSensor, MyAvg, Double>() {
                    @Override
                    public MyAvg createAccumulator() {
                        return new MyAvg();
                    }

                    @Override
                    public MyAvg add(WaterSensor value, MyAvg acc) {
                        //累加器，对元素进行累加
                        acc.sum += value.getVc().longValue();
                        acc.count++;
                        return acc;
                    }

                    @Override
                    public Double getResult(MyAvg acc) {
                        System.out.println("结果输出！");
                        return acc.getAvg();
                    }

                    //这个只有在会话窗口有用，其他窗口都没啥用！！！
                    @Override
                    public MyAvg merge(MyAvg a, MyAvg b) {
                        return null;
                    }
                })
                .print();



        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MyAvg{
        public Long sum = 0L;
        public Long count = 0l;

        //计算平均值
        public Double getAvg(){
            return sum * 1.0/count;
        }
    }
}
